package com.jjc.android.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.SimpleOnPageChangeListener
import com.jjc.android.R
import com.jjc.android.adapter.SimpleFragmentPagerAdapter
import com.jjc.android.base.BaseActivity
import com.jjc.android.dialog.MessageImageDialog
import com.jjc.android.entity.ChangeCardEvent
import com.jjc.android.entity.DetectionBackEvent
import com.jjc.android.entity.DetectionInfo
import com.jjc.android.entity.ScanCardDto
import com.jjc.android.fragment.BindReagentCardFragment
import com.jjc.android.fragment.FillProductInfoFragment
import com.jjc.android.fragment.SamplingCountDownFragment
import com.jjc.android.fragment.AddSamplingFragment
import com.jjc.android.fragment.InsertCardFragment
import com.jjc.android.fragment.OnDetectionPageListener
import com.jjc.android.fragment.SamplingFragment
import com.jjc.android.fragment.SamplingTakePhotoFragment
import com.jjc.android.util.AndroidBug5497Workaround
import com.jjc.android.view.onClick
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DetectionActivity : BaseActivity(){

    companion object{
        fun start(context: Context,testerId:String?,deviceSn:String){
            val intent = Intent(context,DetectionActivity::class.java)
            intent.putExtra("testerId",testerId)
            intent.putExtra("deviceSn",deviceSn)
            context.startActivity(intent)
        }
    }

    override val layoutId: Int = R.layout.activity_detection

    private val viewPager: ViewPager by lazy { findViewById<ViewPager>(R.id.viewPager) }
    private val step5Txt: TextView by lazy { findViewById<TextView>(R.id.step5Txt) }
    private val dot5View: ImageView by lazy { findViewById<ImageView>(R.id.dot5View) }
    private val line4View: View by lazy { findViewById<View>(R.id.line4View) }
    private val step4Txt: TextView by lazy { findViewById<TextView>(R.id.step4Txt) }
    private val dot4View: ImageView by lazy { findViewById<ImageView>(R.id.dot4View) }
    private val line3View: View by lazy { findViewById<View>(R.id.line3View) }
    private val step3Txt: TextView by lazy { findViewById<TextView>(R.id.step3Txt) }
    private val dot3View: ImageView by lazy { findViewById<ImageView>(R.id.dot3View) }
    private val line2View: View by lazy { findViewById<View>(R.id.line2View) }
    private val step2Txt: TextView by lazy { findViewById<TextView>(R.id.step2Txt) }
    private val dot2View: ImageView by lazy { findViewById<ImageView>(R.id.dot2View) }
    private val line1View: View by lazy { findViewById<View>(R.id.line1View) }
    private val step1Txt: TextView by lazy { findViewById<TextView>(R.id.step1Txt) }
    private val dot1View: ImageView by lazy { findViewById<ImageView>(R.id.dot1View) }
    private val homeBtn: ImageView by lazy { findViewById<ImageView>(R.id.homeBtn) }
    private val PAGE_SIZE = 5
    private var testerId:String? = null
    private var deviceSn:String = ""
    override fun initView(savedInstanceState: Bundle?) {
        AndroidBug5497Workaround.assistActivity(this)
        viewPager.adapter = SimpleFragmentPagerAdapter(supportFragmentManager,
            listOf(
                BindReagentCardFragment.newInstance(testerId,deviceSn),
                FillProductInfoFragment.newInstance(testerId,deviceSn),
                SamplingFragment.newInstance(),
                SamplingCountDownFragment.newInstance(),
                InsertCardFragment.newInstance()
            )
        )
        viewPager.offscreenPageLimit = PAGE_SIZE
        showCurrentStatus(0)
        viewPager.addOnPageChangeListener(object : SimpleOnPageChangeListener(){
            override fun onPageSelected(position: Int) {
                showCurrentStatus(position)
            }
        })
        homeBtn.onClick {
            MessageImageDialog.Builder(this)
                .imageResId(R.mipmap.alert_jiancejindu_pic)
                .content(getString(R.string.dete_not_save_txt))
                .okClick {
                    MainActivity.start(this)
                }
                .build()
                .show()
        }
    }

    override fun onBackBtnClick() {
        onBackPressed()
    }

    override fun onBackPressed() {
        if(viewPager.currentItem==0){
            super.onBackPressed()
        }else{
            if(viewPager.currentItem>=3){
                EventBus.getDefault().post(DetectionBackEvent())
            }
            viewPager.currentItem = viewPager.currentItem-1
        }

    }

    private var scanCardDto = ScanCardDto()

    fun saveScanCardInfo(info:ScanCardDto){
        this.scanCardDto = info
    }

    fun getScanCardInfo():ScanCardDto{
        return scanCardDto
    }

    private var detectionInfo = DetectionInfo()
    fun saveDetectionInfo(info:DetectionInfo){
        this.detectionInfo = info
    }

    fun getDetectionInfo():DetectionInfo{
        return detectionInfo
    }

    fun nextStep(){
        if(viewPager.currentItem==PAGE_SIZE-1) return
        viewPager.currentItem = viewPager.currentItem+1
        ((viewPager.adapter as? SimpleFragmentPagerAdapter)
            ?.fragments?.get(viewPager.currentItem) as? OnDetectionPageListener)
            ?.onCurrentStepInvoke()
    }

    override fun setupArguments(args: Bundle?) {
        testerId = args?.getString("testerId")
        deviceSn = args?.getString("deviceSn")?:""
    }

    override fun isHaveEventBus(): Boolean {
        return true
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: ChangeCardEvent){
        viewPager.currentItem = 2
    }

    private fun showCurrentStatus(index:Int){
        setActivityTitle(when(index){
            0->getString(R.string.scan_code_txt)
            1->getString(R.string.tester_and_info_txt)
            2->getString(R.string.sampling_2_txt)
            3->getString(R.string.wait_txt)
            else->getString(R.string.insert_device_txt)
        })
        resetStatus()
        if(index==0){
            homeBtn.visibility = View.GONE
        }else{
            homeBtn.visibility = View.VISIBLE
        }
        when(index){
            0->{
                dot1View.isActivated = true
                step1Txt.isActivated = true
            }
            1->{
                dot1View.isSelected = true
                line1View.isActivated = true
                dot2View.isActivated = true
                step2Txt.isActivated = true
            }
            2->{
                dot1View.isSelected = true
                line1View.isActivated = true
                dot2View.isSelected = true
                line2View.isActivated = true
                dot3View.isActivated = true
                step3Txt.isActivated = true
            }
            3->{
                dot1View.isSelected = true
                line1View.isActivated = true
                dot2View.isSelected = true
                line2View.isActivated = true
                dot3View.isSelected = true
                line3View.isActivated = true
                dot4View.isActivated = true
                step4Txt.isActivated = true
            }
            else->{
                dot1View.isSelected = true
                line1View.isActivated = true
                dot2View.isSelected = true
                line2View.isActivated = true
                dot3View.isSelected = true
                line3View.isActivated = true
                dot4View.isSelected = true
                line4View.isActivated = true
                dot5View.isActivated = true
                step5Txt.isActivated = true
            }
        }
    }

    private fun resetStatus(){
        dot1View.isActivated = false
        dot1View.isSelected = false
        step1Txt.isActivated = false
        line1View.isActivated = false
        dot2View.isActivated = false
        dot2View.isSelected = false
        step2Txt.isActivated = false
        line2View.isActivated = false
        dot3View.isActivated = false
        dot3View.isSelected = false
        step3Txt.isActivated = false
        line3View.isActivated = false
        dot4View.isActivated = false
        dot4View.isSelected = false
        step4Txt.isActivated = false
        line4View.isActivated = false
        dot5View.isActivated = false
        step5Txt.isActivated = false
    }
}